package com.hxalex.simulationhandle.util;

import android.util.Log;

import com.hxalex.simulationhandle.common.SimulationHandleApplication;
import com.hxalex.simulationhandle.db.KeyWordBeanDao;
import com.hxalex.simulationhandle.db.TaskBeanDao;
import com.hxalex.simulationhandle.db.WebSiteBeanDao;
import com.hxalex.simulationhandle.ui.home.model.KeyWordBean;
import com.hxalex.simulationhandle.ui.home.model.TaskBean;
import com.hxalex.simulationhandle.ui.home.model.WebSiteBean;

import java.util.List;

/**
 * Created by hx_Alex on 2018/5/22.
 */

public class DatabaseUtil {
    private static TaskBeanDao taskBeanDao= SimulationHandleApplication.getInstance().getDaoSession().getTaskBeanDao();
    private static KeyWordBeanDao keyWordBeanDao= SimulationHandleApplication.getInstance().getDaoSession().getKeyWordBeanDao();
    private static WebSiteBeanDao webSiteBeanDao= SimulationHandleApplication.getInstance().getDaoSession().getWebSiteBeanDao();
    
    
    public  static Long insertTaskInfo(TaskBean taskBean)
    {
        if (taskBean == null) return -1l;
         return  taskBeanDao.insertOrReplace(taskBean);
    }

   
    public  static boolean insertkeyWordBeanList(List<KeyWordBean> keyWordBeans, TaskBean taskBean)
    {
        if (keyWordBeans==null || taskBean == null) return false;
        for (KeyWordBean bean:keyWordBeans)
        {
            bean.setTaskId(taskBean.getTaskId());
        }
        
        try {
            keyWordBeanDao.insertOrReplaceInTx(keyWordBeans);
        }catch (Exception ex)
        {
            return false;
        }
        return true;  
    }

    public  static boolean insertkeyWebSiteBeanList(List<WebSiteBean> webSiteBeans, TaskBean taskBean)
    {
        if (webSiteBeans==null || taskBean == null) return false;
        for (WebSiteBean bean:webSiteBeans)
        {
            bean.setTaskId(taskBean.getTaskId());
        }

        try {
            webSiteBeanDao.insertOrReplaceInTx(webSiteBeans);
        }catch (Exception ex)
        {
            return false;
        }
        return true;
    }
    
    
    public static List<TaskBean> selectAllTaskBean()
    {
        return taskBeanDao.queryBuilder().list();
    }

    
    public static TaskBean selectAllTaskBean(TaskBean taskBean)
    {
        return taskBeanDao.queryBuilder().where(TaskBeanDao.Properties.TaskId.eq(taskBean.getTaskId())).unique();
    }
    
    public static List<KeyWordBean> selectKeyWordByTaskId(TaskBean bean)
    {
        return keyWordBeanDao.queryBuilder().where(KeyWordBeanDao.Properties.TaskId.eq(bean.getTaskId())).list();
    }

    public static List<WebSiteBean> selectWebSiteByTaskId(TaskBean bean)
    {
        return webSiteBeanDao.queryBuilder().where(WebSiteBeanDao.Properties.TaskId.eq(bean.getTaskId())).list();
    }
    
    
    public static boolean deleteTask(TaskBean taskBean)
    {
        try 
        {
            taskBeanDao.deleteByKey(taskBean.getTaskId());
            List<KeyWordBean> list = keyWordBeanDao.queryBuilder().where(KeyWordBeanDao.Properties.TaskId.eq(taskBean.getTaskId())).list();
            List<WebSiteBean> list1 = webSiteBeanDao.queryBuilder().where(WebSiteBeanDao.Properties.TaskId.eq(taskBean.getTaskId())).list();
            for(KeyWordBean bean:list)
            {
                keyWordBeanDao.delete(bean);
            }
            for(WebSiteBean bean:list1)
            {
                webSiteBeanDao.delete(bean);
            }    
        }catch (Exception ex)
        {
            return false;
        }
        return true;
    }
    
    
    public static void updateTaskInfo(TaskBean taskBean,List<KeyWordBean> keyWordBeans,List<WebSiteBean> webSiteBeans,boolean isUpdateKeyword,boolean isUpdateWebSite)
    {
        taskBeanDao.update(taskBean);
        
        if(isUpdateKeyword)
        {
            List<KeyWordBean> list = keyWordBeanDao.queryBuilder().where(KeyWordBeanDao.Properties.TaskId.eq(taskBean.getTaskId())).list();
            for(KeyWordBean bean:list)
            {
                keyWordBeanDao.delete(bean);
            }
            insertkeyWordBeanList(keyWordBeans,taskBean);
            Log.i("msg","更新了关键词");
        }
        if(isUpdateWebSite)
        {
            List<WebSiteBean> list1 = webSiteBeanDao.queryBuilder().where(WebSiteBeanDao.Properties.TaskId.eq(taskBean.getTaskId())).list();

            for(WebSiteBean bean:list1)
            {
                webSiteBeanDao.delete(bean);
            }
            insertkeyWebSiteBeanList(webSiteBeans, taskBean);
            Log.i("msg","更新了网站库");
        }
      
     
       
        
        
    }
    
}
